/**
 * @author denis.onishchenko@gmail.com
 */

package com.prapp.mediag;

import java.text.MessageFormat;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.feeder.Mailer;

public final class EmailActivity extends Activity {

    private PRApplication application;
    private int tabIndex;

    private Handler handler = new Handler();

    private ProgressDialog sendDialog;
    private Runnable sendJob = new Runnable() {
        public void run() {
            Config.EmailTab emailTab = (Config.EmailTab) application.getTab(tabIndex);

            try {
                String login = emailTab.getLogin();
                String pass = emailTab.getPass();
                String smtp = emailTab.getSmtp();

                List<Config.Email> emailList = emailTab.getEmailList();
                if (emailList == null) return;

                String name = getTextContent(R.id.etName);
                String email = getTextContent(R.id.etEmail);
                String theme = getTextContent(R.id.etTheme);
                String text = getTextContent(R.id.etText);

                for (Config.Email cEmail : emailList) {
                    if (cEmail == null) continue;
                    Mailer.send(login, pass, smtp, email, name, cEmail.getTo(), cEmail.getCc(), MessageFormat.format(cEmail.getSubject(), theme), text + "\nFrom " + email);
                }

                handler.post(new Runnable() {
                    public void run() {
                        sendDialog.dismiss();
                        clearTextContent(R.id.etName);
                        clearTextContent(R.id.etEmail);
                        clearTextContent(R.id.etTheme);
                        clearTextContent(R.id.etText);
                        Toast.makeText(EmailActivity.this, R.string.YourMessageWasSentSuccessfullyThankYou, Toast.LENGTH_SHORT).show();
                    }
                });
            }
            catch (Throwable x) {
                handler.post(new Runnable() {
                    public void run() {
                        sendDialog.dismiss();
                        new AlertDialog.Builder(EmailActivity.this).setMessage(R.string.ErrorSendingMessage).setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).show();
                    }
                });
            }
        }
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.email);

        Bundle extras = getIntent().getExtras();
        tabIndex = extras.getInt(PRApplication.TAB_INDEX);

        application = (PRApplication) getApplication();

        Config.EmailTab emailTab = (Config.EmailTab) application.getTab(tabIndex);

        String title = emailTab.getName();
        if (title != null) {
            TextView textView = (TextView) findViewById(R.id.tvTitle);
            textView.setText(title);
        }

        String html = emailTab.getHtml();
        if (html != null) {
            TextView textView = (TextView) findViewById(R.id.tvContent);
            textView.setText(html);
        }

        Button button = (Button) findViewById(R.id.bSendEmail);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                onSendEmail();
            }
        });
    }

    private String getTextContent(int id) {
        EditText editText = (EditText) findViewById(id);
        if (editText == null) return null;

        Editable editable = editText.getText();
        if (editable == null) return null;

        return editable.toString();
    }

    private void clearTextContent(int id) {
        EditText editText = (EditText) findViewById(id);
        if (editText == null) return;

        editText.setText("");
    }

    private void onSendEmail() {
        sendDialog = ProgressDialog.show(this, "", getString(R.string.SendingPleaseWait), true, false);
        new Thread(sendJob).start();
    }
}
